package com.jye.rseven.user.controller;


import com.jye.rseven.domain.*;
import com.jye.rseven.domain.CreditRecord;
import com.jye.rseven.service.CreditService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/credit")
@Api(tags="积分控制器API")
public class CreditController {

    @Autowired
    CreditService creditService;


    /*
     * 通过用户id查询我的积分
     *
     * */
    @GetMapping("/findMyCreditByUserId")
    @ApiOperation(value="查询我的积分", notes="根据用户id查询我的积分", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyCreditByUserId( int uId){
        JsonResult jsonResult = new JsonResult();
        Credit credit = creditService.findMyCreditByUserId(uId);
        if (!StringUtils.isEmpty(credit)){
            jsonResult.setData(credit);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 添加数据到积分表
     * */
    @PostMapping("/addCredit")
    @ApiOperation(value = "添加数据到积分表", notes = "根据积分实体添加数据到积分表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "credit",
                    value = "积分实体类", required = true),
    })
    public JsonResult addCredit(Credit credit) {
        JsonResult jsonResult = new JsonResult();
        int i = creditService.addCredit(credit);
        if (i == 1) {
            jsonResult.setMsg("添加成功!");

        } else {
            jsonResult.setMsg("添加失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;

    }

    /*
     * 通过用户id查询我的历史积分
     *
     * */
    @GetMapping("/findMyCreditRecordByuId")
    @ApiOperation(value="查询我的历史积分", notes="根据用户id查询我的历史积分", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyCreditRecordByuId( int uId){
        JsonResult jsonResult = new JsonResult();
        List<CreditRecord> creditList = creditService.findMyCreditRecordByuId(uId);
        if (!StringUtils.isEmpty(creditList)){
            jsonResult.setData(creditList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 通过用户id查询用户上个月积分总数
     *
     * */
    @GetMapping("/findMyCreditRecord")
    @ApiOperation(value="查询我的上个月积分总数", notes="根据用户id查询我的上个月积分总数", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyCreditRecord( int uId){
        JsonResult jsonResult = new JsonResult();
        CreditRecord creditList = creditService.findMyCreditRecord(uId);
        if (!StringUtils.isEmpty(creditList)){
            jsonResult.setData(creditList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 推广成功，增加佣金
     *
     * */

    @PutMapping(value = "/addChargeByuId")
    @ApiOperation(value="发放佣金", notes="用户每推广成功一个用户增加50元佣金", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult addChargeByuId( int uId){
        JsonResult jsonResult = new JsonResult();
        int i=creditService.addChargeByuId(uId);
        if(i == 1){
            jsonResult.setMsg("推广成功!获得佣金");

        }else{
            jsonResult.setMsg("推广失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }
    /*
     * 积分兑换金币
     *
     * */
    @PutMapping(value="/updateCredit")
    @ApiOperation(value="积分兑换金币", notes="10积分换1金币", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "credit",
                    value = "用户积分", required = true,
                    dataType = "Integer"),
    })
    public JsonResult updateCredit( int uId,int credit){
        JsonResult jsonResult = new JsonResult();
        int i  = creditService.updateCredit(uId,credit);
        if(i == 1){
            jsonResult.setMsg("兑换金币成功!");

        }else{
            jsonResult.setMsg("兑换金币失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    /*
     * 提现佣金
     *
     * */
    @PutMapping(value="/updateCharge")
    @ApiOperation(value="提现佣金", notes="提现全部佣金", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult updateCharge( int uId){
        JsonResult jsonResult = new JsonResult();
        int i  = creditService.updateCharge(uId);
        if(i == 1){
            jsonResult.setMsg("提现成功!");

        }else{
            jsonResult.setMsg("提现失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    /*
     * 完成任务，获得相应积分
     *
     * */

    @PutMapping(value = "/addCreditByuId")
    @ApiOperation(value="用户获得积分", notes="完成任务，获得相应积分", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult addCreditByuId( int uId){
        JsonResult jsonResult = new JsonResult();
        int i=creditService.addCreditByuId(uId);
        if(i == 1){
            jsonResult.setMsg("任务完成!获得积分");

        }else{
            //jsonResult.setMsg("获取积分失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @GetMapping("/findMyCreditRecordByUId")
    @ApiOperation(value="查询我的积分历史记录", notes="根据用户id查询查询我的积分历史记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyCreditRecordByUId(int uId){
        JsonResult jsonResult = new JsonResult();
        List<CreditRecord> creditRecords = creditService.findMyCreditRecordByUId(uId);
        if(creditRecords.size()!=0){
            jsonResult.setData(creditRecords);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("暂无积分记录!");
        }
        return jsonResult;
    }

}
